package com.ssyanhuo.arknightshelper.module;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.graphics.drawable.PaintDrawable;
import android.os.Build;
import android.os.Handler;
import android.text.SpannableStringBuilder;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ssyanhuo.arknightshelper.R;
import com.ssyanhuo.arknightshelper.service.OverlayService;
import com.ssyanhuo.arknightshelper.utils.ScreenUtils;
import com.ssyanhuo.arknightshelper.utils.FileUtils;
import com.ssyanhuo.arknightshelper.widget.ChildScrollView;
import com.ssyanhuo.arknightshelper.widget.LineWrapLayout;
import com.ssyanhuo.arknightshelper.widget.TableItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class Drop {
    final static String TAG = "Material";
    JSONObject dataMatrix;
    JSONObject dataMaterial;
    JSONArray dataItems;
    JSONArray dataStages;
    Context applicationContext;
    View contentView;
    Handler handler;
    RelativeLayout parentView;
    ArrayList<RadioButton> radioButtons = new ArrayList<>();
    Map<String, String> stageMap = new HashMap<>();
    Map<String, Integer> costMap = new HashMap<>();
    boolean isAltSelector = false;
    SharedPreferences sharedPreferences;
    OverlayService service;

    public void init(final Context context, View view, OverlayService service){
        applicationContext = context;
        contentView = view;
        this.service = service;
        handler = new Handler();
        sharedPreferences = applicationContext.getSharedPreferences("com.ssyanhuo.arknightshelper_preferences", Context.MODE_PRIVATE);
        try{
            dataMatrix = JSON.parseObject(FileUtils.readData("matrix.json", applicationContext));
            dataMaterial = JSON.parseObject(FileUtils.readData("material.json", applicationContext));
            //下面两个是数组形式
            dataItems = JSON.parseArray(FileUtils.readData("items.json", applicationContext));
            dataStages = JSON.parseArray(FileUtils.readData("stages.json", applicationContext));
        } catch (Exception e){
            e.printStackTrace();
            Log.e(TAG, String.valueOf(e));
            return;
        }

        for (int i = 0; i < dataStages.size(); i++){
            JSONObject object = dataStages.getJSONObject(i);
            stageMap.put(object.getString("stageId"), object.getString("code"));
            costMap.put(object.getString("stageId"), object.getInteger("apCost"));
        }

        getAllRadioButtons(contentView);
        for (int i = 0; i < radioButtons.size(); i++){
            radioButtons.get(i).setOnCheckedChangeListener((buttonView, isChecked) -> {
                if (isChecked){
                    for (int j = 0; j < radioButtons.size(); j++){
                        radioButtons.get(j).setChecked(false);
                    }
                    buttonView.setChecked(true);
                    Drawable[] drawables = new Drawable[2];
                    PaintDrawable paintDrawable = new PaintDrawable(Color.argb(127, 255,255,255));
                    paintDrawable.setCornerRadius(8);
                    drawables[0] = paintDrawable;
                    drawables[1] = buttonView.getBackground();
                    LayerDrawable layerDrawable = new LayerDrawable(drawables);
                    buttonView.setBackground(layerDrawable);
                }else {
                    LayerDrawable layerDrawable = (LayerDrawable) buttonView.getBackground();
                    buttonView.setBackground(layerDrawable.getDrawable(1)); }
                int item = Integer.parseInt(buttonView.getTag().toString());
                ArrayList<JSONObject> result = getResult(item);
                if (result == null){
                    return;
                }
                showResult(item, result);
            });
        }
        SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(applicationContext.getResources().getString(R.string.drop_desc_part_1));
        ((TextView)contentView.findViewById(R.id.drop_description)).setText(spannableStringBuilder);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
            ((TextView)contentView.findViewById(R.id.drop_description)).append("\n" + applicationContext.getString(R.string.get_permission_background_activity));
        }
        ((TextView)contentView.findViewById(R.id.drop_description)).setMovementMethod(LinkMovementMethod.getInstance());
    }

    public ArrayList<JSONObject> getResult(int item){
        ArrayList<JSONObject> result = new ArrayList<>();
        JSONArray matrix = dataMatrix.getJSONArray("matrix");

        for (int i= 0; i < matrix.size(); i++){
            int id = -1;
            try {//部分id并不是int
                id = matrix.getJSONObject(i).getInteger("itemId");
            }catch (NumberFormatException e){
                continue;
            }
            if (id == item){//TODO 用映射
                JSONObject object = matrix.getJSONObject(i);
                //Log.e(TAG, object.toJSONString());
                float cost = 0;
                try{
                    cost = costMap.get(object.getString("stageId")) * ((float)object.getInteger("times") / (float)object.getInteger("quantity"));
                    object.put("cost", String.valueOf(cost));
                    result.add(object);
                }catch (Exception e){
                    e.printStackTrace();
                    Log.e("",object.toJSONString());
                }
            }
            if(id == -1){
                return null;
            }
        }
        Collections.sort(result, (o1, o2) -> {
            float e1 = o1.getFloat("cost");
            float e2 = o2.getFloat("cost");
            if (e1 > e2){return 1;}
            else if(e1 == e2){return 0;}
            else if(e1 < e2){return -1;}
            return 0;
        });
        return result;
    }

    public void showResult(int item, ArrayList<JSONObject> result){
        ArrayList<String> stages = new ArrayList<>();
        ArrayList<Integer> times = new ArrayList<>();
        ArrayList<Integer> quantities = new ArrayList<>();
        ArrayList<Float> costs = new ArrayList<>();
        LinearLayout resultLayout = contentView.findViewById(R.id.drop_result_content);
        resultLayout.removeAllViews();
        TableItem tableTitle = new TableItem(applicationContext, applicationContext.getString(R.string.drop_stage), applicationContext.getString(R.string.drop_times), applicationContext.getString(R.string.drop_count), applicationContext.getString(R.string.drop_unit_price));
        int padding = applicationContext.getResources().getDimensionPixelSize(R.dimen.activity_horizontal_margin);
        tableTitle.setPadding(0, padding, 0, 0);
        try {
            JSONObject formula = dataMaterial.getJSONObject(String.valueOf(item)).getJSONObject("madeof");
            if (formula != null && formula.size() > 0){
                TextView textView = new TextView(applicationContext);
                textView.setPadding(ScreenUtils.dip2px(applicationContext, 8), padding, padding, 0);
                textView.setText(R.string.drop_synthesis);
                for (Map.Entry<String, Object> entry:
                     formula.entrySet()) {
                    textView.append("\n\t\t" + entry.getKey() + ": " + entry.getValue());
                }
                tableTitle.setPadding(0, 0, 0, 0);
                resultLayout.addView(textView);
            }
        } catch (Exception ignored) {

        }

        resultLayout.addView(tableTitle);
        for (int i = 0; i < result.size(); i++){
            JSONObject object = result.get(i);
            if (object.getInteger("quantity") == 0){
                continue;
            }
            String stageId = object.getString("stageId");
            String stage = stageMap.get(stageId);
            int time = object.getInteger("times");
            int quantity = object.getInteger("quantity");
            float cost = object.getFloat("cost");
            stages.add(stage);
            times.add(time);
            quantities.add(quantity);
            costs.add(cost);
            TableItem tableItem = new TableItem(applicationContext, stage, time, quantity, cost);
            resultLayout.addView(tableItem);
        }
        if (stages.size() == 0){//TODO 修改条件
            resultLayout.removeViewAt(resultLayout.getChildCount() - 1);
            TextView textView =new TextView(applicationContext);
            textView.setText(R.string.drop_nothing_found);
            textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
            resultLayout.addView(textView);
        }
        setAltSelector();
    }

    public void setAltSelector(){
        ScrollView scrollView = contentView.findViewById(R.id.drop_selector_scroll);
        ViewGroup.LayoutParams params = scrollView.getLayoutParams();
        params.height = ScreenUtils.dip2px(applicationContext, 128);
        scrollView.setLayoutParams(params);
        isAltSelector = true;
    }

    public  void getAllRadioButtons(View view){
        ViewGroup viewGroup = (ViewGroup)view;
        for(int i = 0; i < viewGroup.getChildCount(); i++){
            if(viewGroup.getChildAt(i) instanceof RadioButton){
                radioButtons.add((RadioButton) viewGroup.getChildAt(i));
            }else if(viewGroup.getChildAt(i) instanceof LineWrapLayout){
                getAllRadioButtons(viewGroup.getChildAt(i));
            }else if(viewGroup.getChildAt(i) instanceof LinearLayout){
                getAllRadioButtons(viewGroup.getChildAt(i));
            }else if(viewGroup.getChildAt(i) instanceof ScrollView){
                getAllRadioButtons(viewGroup.getChildAt(i));
            }else if(viewGroup.getChildAt(i) instanceof ChildScrollView){
                getAllRadioButtons(viewGroup.getChildAt(i));
            }
        }
    }
}
